<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/2  17:49
 */
namespace app\admin\Controller;
use think\Db;

class Circle extends Common
{
    /*
    *构造函数
   */
    function __construct(){
        parent::__construct();
    }

    //需求圈
    public function index()
    {
        $pageParam = ['query'=>[]];
        $where = [];

        $user_nickname = input('user_nickname');
        $user_id = input('user_id');
        $type = input('type');
        $circle_name = input('circle_name');
        $update_index = input('update_index');

        if(!empty($circle_name)){
            $where['ci.circle_name'] = ['like', '%'.$circle_name.'%'];
            $pageParam['query']['ci.circle_name'] = $circle_name;
        }

        if(!empty($user_nickname)){
            $where['ms.nickname'] = ['like', '%'.$user_nickname.'%'];
            $pageParam['query']['ms.nickname'] = $user_nickname;
        }

        if(!empty($user_id)){
            $where['ms.id'] = $user_id;
            $pageParam['query']['ms.id'] = $user_id;
        }

        if(!empty($type)){
            $where['ci.type'] = $type;
            $pageParam['query']['ci.type'] = $type;
        }

        if($update_index == '1' || $update_index == '0'){
            //首页推荐
            $cid = input('cid/a');
            $where['id'] = ['in',$cid];
            $result = db('circle')->where($where)->update(['is_index'=>$update_index]);

            if(!$result)$this->error('修改失败');
            $this->success('修改成功',url('Circle/index'));
        }

        $where['ci.is_del'] = 1;

        $data = db('circle')->alias('ci')->join('member ms','ms.id=ci.user_id')->where($where)->order('ci.id desc')->field('ci.*,ms.headimage,ms.nickname')->paginate(15,false, $pageParam);

        $this->assign('page', $data->render());
        $this->assign('data', $data);
        $this->assign('user_nickname', $user_nickname);
        $this->assign('user_id', $user_id);
        $this->assign('type', $type);
        $this->assign('circle_name', $circle_name);
        return $this->fetch();
    }
}